package com.liuxinlong.modules.system.service.impl;

import com.liuxinlong.enums.RedisKeyEnum;
import com.liuxinlong.modules.system.service.RedisManageService;
import com.liuxinlong.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 缓存数据信息实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-04-20
 */
@Service
@Slf4j
public class RedisManageServiceImpl implements RedisManageService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<Map<String, Object>> pageRedisInfo(Map<String, Object> queryParam) {
//        Set<String> keySet= redisUtils.getAllKeys();
        List<Map<String, Object>> resultList = new ArrayList<>();
        int sort = 1;
//        for (String key : keySet) {
//            Map<String,Object> item = new HashMap<>();
//            item.put("sort",sort);
//            item.put("cacheKey",key);
//            resultList.add(item);
//            sort ++;
//        }
        for (RedisKeyEnum keyEnum : RedisKeyEnum.values()) {
            Map<String, Object> item = new HashMap<>();
            item.put("sort", sort);
            item.put("id", keyEnum.getValue());
            item.put("cacheKey", keyEnum.getKey());
            item.put("cacheName", keyEnum.getName());
            resultList.add(item);
            sort++;
        }
        return resultList;
    }

    @Override
    public int getRedisCount(Map<String, Object> queryParam) {
        return RedisKeyEnum.values().length;
    }

    @Override
    public void deleteRedisInfo(String id) {
        redisUtils.clearAllCache();
    }

    @Override
    public void clearRedisInfo(String id, String subKey) {
        String key = RedisKeyEnum.getKeyByValue(Integer.valueOf(id));
        if (StringUtils.isEmpty(subKey)) {
            redisUtils.clearKeyGroupCache(key);
            return;
        }
        redisUtils.clearKeyCache(subKey);
    }

    @Override
    public List<Map<String, Object>> queryRedisDetailInfo(String id) {
        int type = Integer.valueOf(id);
        String groupKey = RedisKeyEnum.getKeyByValue(type);
        int sort = 1;
        List<Map<String, Object>> dataList;
        if (type == RedisKeyEnum.FORUM_STATISTICS_CACHE.getValue()) {
            Map<Object, Object> forumCache = redisUtils.getForumCache();
            dataList = new ArrayList<>(forumCache.size());
            for (Object forumKey :forumCache.keySet()){
                Map<String,Object> map = new HashMap<>();
                map.put("sort",sort);
                map.put("cacheKey",forumKey);
                map.put("cacheValue",forumCache.get(forumKey));
                dataList.add(map);
                sort++;
            }
        } else {
            Set<String> keys = redisUtils.getKeyGroupCache(groupKey);
             dataList = new ArrayList<>(keys.size());
            for (String key : keys){
                Map<String,Object> map = new HashMap<>();
                map.put("sort",sort);
                map.put("cacheKey",key);
                map.put("cacheValue",redisUtils.getKeyValue(key));
                dataList.add(map);
                sort++;
            }
        }
        return dataList;
    }
}
